/*******************************************************************************
 * Copyright 2011 See AUTHORS file.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

package com.goldsprite.gdxcore.utils;

import com.badlogic.gdx.*;
import com.badlogic.gdx.math.*;

import com.badlogic.gdx.graphics.OrthographicCamera;
import com.goldsprite.gdxcore.logs.LogViewerService;

public class OrthoCamController extends InputAdapter {
    final OrthographicCamera camera;
    final Vector3 curr = new Vector3();
    final Vector3 last = new Vector3(-1, -1, -1);
    final Vector3 delta = new Vector3();

    private float initialDistance = 0f;
    private float currentDistance = 0f;
    private boolean isZooming = false;

	public float moveRate = 1.2f;
    private final float minZoom = 0.5f; // 最小缩放
    private final float maxZoom = 2.0f; // 最大缩放

	private boolean[] isUIEvent;

    public OrthoCamController(OrthographicCamera camera, boolean[] isUIEvent) {
        this.camera = camera;
		this.isUIEvent = isUIEvent;
    }

	Vector3 tmpPos = new Vector3();
    @Override
    public boolean touchDragged(int x, int y, int pointer) {
		//LogViewerService.logDebug("[游戏世界相机控制器] 拖动画布");
		if(isUIEvent[0]) return false;
		if (Gdx.input.isTouched(0) && Gdx.input.isTouched(1)) {
            // 双指触控，处理缩放
            int x1 = Gdx.input.getX(0);
            int y1 = Gdx.input.getY(0);
            int x2 = Gdx.input.getX(1);
            int y2 = Gdx.input.getY(1);

            if (!isZooming) {
                // 初始化双指缩放状态
                initialDistance = calculateDistance(x1, y1, x2, y2);
                isZooming = true;
            }
			else {
                // 计算缩放
                currentDistance = calculateDistance(x1, y1, x2, y2);
                float zoomFactor = initialDistance / currentDistance;
               	camera.zoom *= zoomFactor;
				//限制大小
				//camera.zoom = MathUtils.clamp(camera.zoom, minZoom, maxZoom);
                initialDistance = currentDistance;
            }
        }
		else if(!isZooming) {
            // 单指拖动，处理平移
            camera.unproject(curr.set(x, y, 0));
            if (!(last.x == -1 && last.y == -1 && last.z == -1)) {
                camera.unproject(delta.set(last.x, last.y, 0));
                delta.sub(curr);
                camera.position.add(delta.x*moveRate, delta.y*moveRate, 0);
            }
            last.set(x, y, 0);
        }
        return false;
    }

    @Override
    public boolean touchDown(int x, int y, int pointer, int button) {
		//LogViewerService.logDebug("[游戏世界相机控制器] 按下画布");
        if(!isUIEvent[0]){
			camera.unproject(tmpPos.set(x, y, 0));
			//LogViewerService.log(String.format("触摸坐标: 屏幕: %d, %d, 世界: %s", x, y, tmpPos));
		}
		// 重置触控状态
        if (Gdx.input.isTouched(0) && Gdx.input.isTouched(1)) {
            int x1 = Gdx.input.getX(0);
            int y1 = Gdx.input.getY(0);
            int x2 = Gdx.input.getX(1);
            int y2 = Gdx.input.getY(1);
            initialDistance = calculateDistance(x1, y1, x2, y2);
            isZooming = true;
        }
        return false;
    }

    @Override
    public boolean touchUp(int x, int y, int pointer, int button) {
        // 停止缩放或拖动
        if (!Gdx.input.isTouched(0) || !Gdx.input.isTouched(1)) {
            isZooming = false;
        }
        last.set(-1, -1, -1);
        return false;
    }

	////pc

	// 添加到现有方法中
	@Override
	public boolean keyDown(int keycode) {
		if (keycode == Input.Keys.CONTROL_LEFT) {
			isZooming = true;
			//logViewer.logInfo("进入缩放模式 (LeftCtrl 按下)");
		}
		return false;
	}

	@Override
	public boolean keyUp(int keycode) {
		if (keycode == Input.Keys.CONTROL_LEFT) {
			isZooming = false;
			//logViewer.logInfo("退出缩放模式 (LeftCtrl 松开)");
		}
		return false;
	}

	@Override
	public boolean scrolled(float amountX, float amountY) {
		if (isZooming) {
			float zoomStep = 0.1f;
			camera.zoom *= (1+amountY * zoomStep);
			//camera.zoom = MathUtils.clamp(camera.zoom, minZoom, maxZoom);

			//logViewer.logInfo(String.format("滚轮缩放: Zoom = %.2f", camera.zoom));
			return true;
		}
		return false;
	}


	private float calculateDistance(int x1, int y1, int x2, int y2) {
        return (float) Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
    }
}

